CASE_3

Liam Phan, Michael Bigler, Tania Loureiro, William Elkiess, Dakota Cuellar and Ilyana El Mendili

2023-05-07

Packages

library(DT)
library(adabag)
library(rpart.plot)
library(pROC)
library(summarytools)
library(corrplot)
library(dplyr)
library(GGally)
library(fastDummies)
library(ggcorrplot)
library(klaR)
library(psych)
library(MASS)
library(devtools)
library(ggplot2)
library(ggthemes)
library(GGally)
library(caret)
library(splitTools)
library(rpart)
library(xgboost)
library(caTools)
library(dplyr)
library(caret)
library(naniar)
library(kableExtra)

CM_Function <- function(cm) {

  layout(matrix(c(1,1,2)))
  par(mar=c(2,2,2,2))
  plot(c(100, 345), c(300, 450), type = "n", xlab="", ylab="", xaxt='n', yaxt='n')
  title('CONFUSION MATRIX', cex.main=2)

  # create the matrix 
  rect(150, 430, 240, 370, col='#2F4F4E')
  text(195, 435, 'No', cex=1.2)
  rect(250, 430, 340, 370, col='#0D8387')
  text(295, 435, 'Yes', cex=1.2)
  text(125, 370, 'Predicted', cex=1.3, srt=90, font=2)
  text(245, 450, 'Actual', cex=1.3, font=2)
  rect(150, 305, 240, 365, col='#0D8387')
  rect(250, 305, 340, 365, col='#2F4F4E')
  text(140, 400, 'No', cex=1.2, srt=90)
  text(140, 335, 'Yes', cex=1.2, srt=90)

  # add in the cm results 
  res <- as.numeric(cm$table)
  text(195, 400, res[1], cex=1.6, font=2, col='white')
  text(195, 335, res[2], cex=1.6, font=2, col='white')
  text(295, 400, res[3], cex=1.6, font=2, col='white')
  text(295, 335, res[4], cex=1.6, font=2, col='white')

  # add in the specifics 
  plot(c(100, 0), c(100, 0), type = "n", xlab="", ylab="", main = "DETAILS", xaxt='n', yaxt='n')
  text(10, 85, names(cm$byClass[1]), cex=1.2, font=2)
  text(10, 70, round(as.numeric(cm$byClass[1]), 3), cex=1.2)
  text(30, 85, names(cm$byClass[2]), cex=1.2, font=2)
  text(30, 70, round(as.numeric(cm$byClass[2]), 3), cex=1.2)
  text(50, 85, names(cm$byClass[5]), cex=1.2, font=2)
  text(50, 70, round(as.numeric(cm$byClass[5]), 3), cex=1.2)
  text(70, 85, names(cm$byClass[6]), cex=1.2, font=2)
  text(70, 70, round(as.numeric(cm$byClass[6]), 3), cex=1.2)
  text(90, 85, names(cm$byClass[7]), cex=1.2, font=2)
  text(90, 70, round(as.numeric(cm$byClass[7]), 3), cex=1.2)

  # add in the accuracy information 
  text(30, 35, names(cm$overall[1]), cex=1.5, font=2)
  text(30, 20, round(as.numeric(cm$overall[1]), 3), cex=1.4)
  text(70, 35, names(cm$overall[2]), cex=1.5, font=2)
  text(70, 20, round(as.numeric(cm$overall[2]), 3), cex=1.4)
}  

DATA AND QUICK FACTORING

df <- readxl::read_xls('Cchurn.xls')
df$international_plan <- factor(df$international_plan, levels = c('no', 'yes'), labels = c('0','1'))
df$voice_mail_plan <- factor(df$voice_mail_plan, levels = c('no', 'yes'), labels = c('0','1'))
df$churn <- factor(df$churn, levels = c('no', 'yes'), labels = c('0','1'))

SUMMARY

print(summarytools::dfSummary(df), method = 'render')

Data Frame Summary

df

Dimensions: 5000 x 18
Duplicates: 0
No Variable Stats / Values Freqs (% of Valid) Graph Valid Missing
1 account_length [numeric]
Mean (sd) : 100.3 (39.7)
min ≤ med ≤ max:
1 ≤ 100 ≤ 243
IQR (CV) : 54 (0.4)
218 distinct values 5000 (100.0%) 0 (0.0%)
2 international_plan [factor]
1. 0
2. 1
4527(90.5%)
473(9.5%)
5000 (100.0%) 0 (0.0%)
3 voice_mail_plan [factor]
1. 0
2. 1
3677(73.5%)
1323(26.5%)
5000 (100.0%) 0 (0.0%)
4 number_vmail_messages [numeric]
Mean (sd) : 7.8 (13.5)
min ≤ med ≤ max:
0 ≤ 0 ≤ 52
IQR (CV) : 17 (1.7)
48 distinct values 5000 (100.0%) 0 (0.0%)
5 total_day_minutes [numeric]
Mean (sd) : 180.3 (53.9)
min ≤ med ≤ max:
0 ≤ 180.1 ≤ 351.5
IQR (CV) : 72.5 (0.3)
1961 distinct values 5000 (100.0%) 0 (0.0%)
6 total_day_calls [numeric]
Mean (sd) : 100 (19.8)
min ≤ med ≤ max:
0 ≤ 100 ≤ 165
IQR (CV) : 26 (0.2)
123 distinct values 5000 (100.0%) 0 (0.0%)
7 total_day_charge [numeric]
Mean (sd) : 30.6 (9.2)
min ≤ med ≤ max:
0 ≤ 30.6 ≤ 59.8
IQR (CV) : 12.3 (0.3)
1961 distinct values 5000 (100.0%) 0 (0.0%)
8 total_eve_minutes [numeric]
Mean (sd) : 200.6 (50.6)
min ≤ med ≤ max:
0 ≤ 201 ≤ 363.7
IQR (CV) : 67.7 (0.3)
1879 distinct values 5000 (100.0%) 0 (0.0%)
9 total_eve_calls [numeric]
Mean (sd) : 100.2 (19.8)
min ≤ med ≤ max:
0 ≤ 100 ≤ 170
IQR (CV) : 27 (0.2)
126 distinct values 5000 (100.0%) 0 (0.0%)
10 total_eve_charge [numeric]
Mean (sd) : 17.1 (4.3)
min ≤ med ≤ max:
0 ≤ 17.1 ≤ 30.9
IQR (CV) : 5.8 (0.3)
1659 distinct values 5000 (100.0%) 0 (0.0%)
11 total_night_minutes [numeric]
Mean (sd) : 200.4 (50.5)
min ≤ med ≤ max:
0 ≤ 200.4 ≤ 395
IQR (CV) : 67.8 (0.3)
1853 distinct values 5000 (100.0%) 0 (0.0%)
12 total_night_calls [numeric]
Mean (sd) : 99.9 (20)
min ≤ med ≤ max:
0 ≤ 100 ≤ 175
IQR (CV) : 26 (0.2)
131 distinct values 5000 (100.0%) 0 (0.0%)
13 total_night_charge [numeric]
Mean (sd) : 9 (2.3)
min ≤ med ≤ max:
0 ≤ 9 ≤ 17.8
IQR (CV) : 3.1 (0.3)
1028 distinct values 5000 (100.0%) 0 (0.0%)
14 total_intl_minutes [numeric]
Mean (sd) : 10.3 (2.8)
min ≤ med ≤ max:
0 ≤ 10.3 ≤ 20
IQR (CV) : 3.5 (0.3)
170 distinct values 5000 (100.0%) 0 (0.0%)
15 total_intl_calls [numeric]
Mean (sd) : 4.4 (2.5)
min ≤ med ≤ max:
0 ≤ 4 ≤ 20
IQR (CV) : 3 (0.6)
21 distinct values 5000 (100.0%) 0 (0.0%)
16 total_intl_charge [numeric]
Mean (sd) : 2.8 (0.7)
min ≤ med ≤ max:
0 ≤ 2.8 ≤ 5.4
IQR (CV) : 0.9 (0.3)
170 distinct values 5000 (100.0%) 0 (0.0%)
17 number_customer_service_calls [numeric]
Mean (sd) : 1.6 (1.3)
min ≤ med ≤ max:
0 ≤ 1 ≤ 9
IQR (CV) : 1 (0.8)
0:1023(20.5%)
1:1786(35.7%)
2:1127(22.5%)
3:665(13.3%)
4:252(5.0%)
5:96(1.9%)
6:34(0.7%)
7:13(0.3%)
8:2(0.0%)
9:2(0.0%)
5000 (100.0%) 0 (0.0%)
18 churn [factor]
1. 0
2. 1
4293(85.9%)
707(14.1%)
5000 (100.0%) 0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.3)
2023-05-07

  • We have no missing values -> perfect
  • Heavily uneven counts of dependent variable (86 % no / 14 % yes) -> maybe sample for equality / maybe not because we loose information of other data
  • Independent variables are on different scales -> standardize
  • two (maybe three) categorical predictors: International plan / voice_mail_plan (/ maybe number_customer_service_calls) -> dummy encode -> not necessary as already 0 and 1
  • Rest of data is numeric and most of the variables looks normally distributed with exception of number_vmail_messages and totat_intl_calls
    • transform these value to make them normal?
    • maybe make parts of them categorical? (recieving voice mail or not, calling internationally or not)
    • or maybe the categorical values that we have already give an indication for this
    • Test normality of variables
  • Can variables be combined? We have day / eve / night / intl calls and for each of them minutes / calls / charge. Maybe we can combine this into one metric. Maybe average cost per minute or average cost per call?

CORRELATION PLOT BEFORE DATA ENGINEERING

df_numeric <- select_if(df, is.numeric)  # Subset numeric columns with dplyr

M <- cor(df_numeric)

p.mat <- cor_pmat(df_numeric)

ggcorrplot(M, hc.order = TRUE, type = "lower", lab = TRUE, p.mat = p.mat, sig.level=0.05, lab_size = 2, tl.cex = 10,outline.col = "white", ggtheme = ggplot2::theme_minimal(), colors = c("#2F4F4E", "white", "#0D8387")) 

Proves theory from before -> we can make one metric out of charge and minutes –> charge / minutes

DATA ENGINEERING

df$total_day_charge_per_minute <- ifelse(df$total_day_minutes == 0, 0, df$total_day_charge / df$total_day_minutes)
df$total_eve_charge_per_minute <- ifelse(df$total_eve_minutes == 0, 0, df$total_eve_charge / df$total_eve_minutes)
df$total_night_charge_per_minute <- ifelse(df$total_night_minutes == 0, 0, df$total_night_charge / df$total_night_minutes)
df$total_intl_charge_per_minute <- ifelse(df$total_intl_minutes == 0, 0, df$total_intl_charge / df$total_intl_minutes)
df <- subset(df, select = -c(total_day_charge, total_day_minutes, total_eve_charge, total_eve_minutes, total_night_charge, total_night_minutes, total_intl_charge, total_intl_minutes))

CORRELATION PLOT AFTER DATA ENGINEERING

df_numeric <- select_if(df, is.numeric)  # Subset numeric columns with dplyr

M <- cor(df_numeric)

p.mat <- cor_pmat(df_numeric)

ggcorrplot(M, hc.order = TRUE, type = "lower", lab = TRUE, p.mat = p.mat, sig.level=0.05, lab_size = 2, tl.cex = 10,outline.col = "white", ggtheme = ggplot2::theme_minimal(), colors = c("#2F4F4E", "white", "#0D8387")) 

Now we have non-correlated data

HIGHER ORDER FEATURES

Only squaring as we have no negative data. Cubing would be needed with negative data.

# squared
df2 <- df^2
df2 <- df2[,-c(2,3,10)]
colnames(df2) <- paste0(colnames(df2), '_sqd')

df <- cbind(df,df2)

Relationship between data in higher order

# theme_set(theme_minimal())
# 
# ggpairs(
#   data = df,
#   columns = c(1:9, 11:25),
#   mapping = aes(col = churn, alpha = .9)
# ) +
#   scale_fill_colorblind() +
#   scale_color_colorblind()

SAMPLING METHODS

As we have unbalanced data we need to use a sampling method to balance the classes. Hereby there are four different methods. OVER / UNDER / BOTH / ROSE.

library(ROSE)

# OVER
df_OVER <- ovun.sample(churn~., data = df, method = "over")$data

table(df$churn)
FALSE 
FALSE    0    1 
FALSE 4293  707
table(df_OVER$churn)
FALSE 
FALSE    0    1 
FALSE 4293 4238
# UNDER
df_UNDER <- ovun.sample(churn~., data = df, method = "under")$data

table(df$churn)
FALSE 
FALSE    0    1 
FALSE 4293  707
table(df_UNDER$churn)
FALSE 
FALSE   0   1 
FALSE 670 707
# BOTH
df_BOTH <- ovun.sample(churn~., data = df, method = "both")$data

table(df$churn)
FALSE 
FALSE    0    1 
FALSE 4293  707
table(df_BOTH$churn)
FALSE 
FALSE    0    1 
FALSE 2508 2492
# ROSE
df_ROSE <- ROSE(churn ~ ., data = df, seed = 1, p = 0.5)$data

SAMPLING POST VISUALIZATION

# theme_set(theme_minimal())
# 
# ggpairs(
#   data = df_ROSE,
#   columns = c(1:9, 11:25),
#   mapping = aes(col = churn, alpha = .9)
# ) +
#   scale_fill_colorblind() +
#   scale_color_colorblind() +
#   labs(title = "Machine Learning Project")
# 
# ggpairs(
#   data = df_OVER,
#   columns = c(1:9, 11:25),
#   mapping = aes(col = churn, alpha = .9)
# ) +
#   scale_fill_colorblind() +
#   scale_color_colorblind() +
#   labs(title = "Machine Learning Project")

# ggpairs(
#   data = df_UNDER,
#   columns = c(1:9, 11:25),
#   mapping = aes(col = churn, alpha = .9)
# ) +
#   scale_fill_colorblind() +
#   scale_color_colorblind() +
#   labs(title = "Machine Learning Project")

# ggpairs(
#   data = df_BOTH,
#   columns = c(1:9, 11:25),
#   mapping = aes(col = churn, alpha = .9)
# ) +
#   scale_fill_colorblind() +
#   scale_color_colorblind() +
#   labs(title = "Machine Learning Project")

TRAIN AND TEST SPLIT

As we need to test the models we need to split the sampled data.

set.seed(1)
data <- df_OVER # choose which data to use df_ROSE / df_BOTH / df_UNDER / df_OVER / df
inds <- splitTools::partition(data$churn, p = c(train = 0.7, test = 0.3))
dftrain <- data[inds$train,]
dftest <- data[inds$test,]

SCALING

As some methods need scaled data we scale the data here to be centered.

norm.value <- preProcess(dftrain, method = c("center", "scale"))
dftrain <- predict(norm.value, dftrain)
dftest <- predict(norm.value, dftest)

df_original_test <- predict(norm.value, df)

PREDICTIVE MODELS

BOOSTING

set.seed(123)

# train bagged model
mod.boost <- boosting(churn ~., data=dftrain)

predicted.boost <- factor(predict(mod.boost, dftest, type="class")$class)

PRED_BOOSTING <- predicted.boost

confmat.boost <- confusionMatrix(data=predicted.boost, reference = dftest$churn, positive = '1')

CM_Function(confmat.boost)

roc_score.boost =roc(factor(dftest$churn, ordered=TRUE), factor(predicted.boost, ordered=TRUE))
plot(roc_score.boost ,main ="ROC curve")

BOOSTING ON ORIGINAL DATA

predicted.boost <- factor(predict(mod.boost, df_original_test, type="class")$class)

PRED_BOOSTING_ORIGINAL <- predicted.boost

confmat.boost <- confusionMatrix(data=predicted.boost, reference = df_original_test$churn, positive = '1')

CM_Function(confmat.boost)

roc_score.boost =roc(factor(df_original_test$churn, ordered=TRUE), factor(predicted.boost, ordered=TRUE))
plot(roc_score.boost ,main ="ROC curve")

CTREE

set.seed(123)

tree_full <- rpart(churn ~ ., 
              data = dftrain, 
              method = "class",  # "class" because Y is a binary factor
              minbucket = 1,
              cp = 0.00001) 

# Plot tree
rpart.plot(tree_full, yesno = TRUE, digits =-6)

min_xerr<- which.min(tree_full$cptable[,"xerror"]) # select minimum cross-validation error
cp_bp <- tree_full$cptable[min_xerr,"CP"]  # find the corresponding CP value, to get the "best pruned " tree


mod.pruned_tree<- prune(tree_full, cp = cp_bp) # re-compute the tree with the selected Cp

rpart.plot(mod.pruned_tree, yesno = TRUE, digits =-3)

predicted.pruned_tree <- predict(mod.pruned_tree, dftest[,-c(10)], type = "class")

PRED_CTREE <- predicted.pruned_tree

confmat.prunned_tree <- confusionMatrix(data=predicted.pruned_tree, reference = dftest$churn, positive = '1')

CM_Function(confmat.prunned_tree)

roc_score.prunned_tree =roc(factor(dftest$churn, ordered=TRUE), factor(predicted.pruned_tree, ordered=TRUE))
plot(roc_score.prunned_tree ,main ="ROC curve")

library(rattle)

rpart.plot(mod.pruned_tree, yesno = TRUE, digits =-3)

# Customizing the output
pdf("CTREE.pdf",        
    width = 30, height = 30, 
    bg = "white",          
    colormodel = "rgb")          

# Creating a plot
fancyRpartPlot(mod.pruned_tree,yesno=TRUE,main="Pruned Tree",tweak=3)

# Closing the graphical device
dev.off() 
FALSE quartz_off_screen 
FALSE                 2

CTREE ON ORIGINAL DATA

predicted.pruned_tree <- predict(mod.pruned_tree, df_original_test[,-c(10)], type = "class")

PRED_CTREE_ORIGINAL <- predicted.pruned_tree

confmat.prunned_tree <- confusionMatrix(data=predicted.pruned_tree, reference = df_original_test$churn, positive = '1')

CM_Function(confmat.prunned_tree)

roc_score.prunned_tree =roc(factor(df_original_test$churn, ordered=TRUE), factor(predicted.pruned_tree, ordered=TRUE))
plot(roc_score.prunned_tree ,main ="ROC curve")

VARIABLES IMPORTANCES

relevance<-as.data.frame(mod.pruned_tree$variable.importance) #we get the ranking of the variables by importance
kable(relevance, row.names = T,col.names="Variable Importance") %>% kable_paper("hover", full_width = T) #built table
Variable Importance
total_day_charge_per_minute 570.5836
total_day_charge_per_minute_sqd 483.5076
total_eve_calls 421.4505
number_customer_service_calls 410.3489
total_eve_charge_per_minute 404.0306
number_customer_service_calls_sqd 373.4925
account_length 373.2413
total_eve_calls_sqd 365.3323
total_intl_charge_per_minute 331.1794
total_night_charge_per_minute 328.3523
total_day_calls 327.6633
total_night_calls 320.1356
total_intl_calls 305.0116
total_eve_charge_per_minute_sqd 300.5814
international_plan 280.4252
account_length_sqd 274.0323
total_intl_charge_per_minute_sqd 271.6084
total_day_calls_sqd 263.7161
total_night_charge_per_minute_sqd 259.9630
total_night_calls_sqd 254.8879
total_intl_calls_sqd 254.8427
number_vmail_messages 168.1912
number_vmail_messages_sqd 164.1998
voice_mail_plan 124.7682
relevance
FALSE                                   mod.pruned_tree$variable.importance
FALSE total_day_charge_per_minute                                  570.5836
FALSE total_day_charge_per_minute_sqd                              483.5076
FALSE total_eve_calls                                              421.4505
FALSE number_customer_service_calls                                410.3489
FALSE total_eve_charge_per_minute                                  404.0306
FALSE number_customer_service_calls_sqd                            373.4925
FALSE account_length                                               373.2413
FALSE total_eve_calls_sqd                                          365.3323
FALSE total_intl_charge_per_minute                                 331.1794
FALSE total_night_charge_per_minute                                328.3523
FALSE total_day_calls                                              327.6633
FALSE total_night_calls                                            320.1356
FALSE total_intl_calls                                             305.0116
FALSE total_eve_charge_per_minute_sqd                              300.5814
FALSE international_plan                                           280.4252
FALSE account_length_sqd                                           274.0323
FALSE total_intl_charge_per_minute_sqd                             271.6084
FALSE total_day_calls_sqd                                          263.7161
FALSE total_night_charge_per_minute_sqd                            259.9630
FALSE total_night_calls_sqd                                        254.8879
FALSE total_intl_calls_sqd                                         254.8427
FALSE number_vmail_messages                                        168.1912
FALSE number_vmail_messages_sqd                                    164.1998
FALSE voice_mail_plan                                              124.7682

BAGGING

set.seed(123)
library(ipred)
library(pROC)

# train bagged model
ames_bag1 <- bagging(
  formula = churn ~ .,
  data = dftrain, 
  nbagg = 100,  
  coob = TRUE,
  control = rpart.control(minsplit = 2, cp = 0)
  )

ames_bag1
FALSE 
FALSE Bagging classification trees with 100 bootstrap replications 
FALSE 
FALSE Call: bagging.data.frame(formula = churn ~ ., data = dftrain, nbagg = 100, 
FALSE     coob = TRUE, control = rpart.control(minsplit = 2, cp = 0))
FALSE 
FALSE Out-of-bag estimate of misclassification error:  0.0494
predicted <- factor(ifelse(predict(ames_bag1, dftest[,-c(10)], type = 'prob')[,2] >= 0.5, 1, 0))

PRED_BAGGING <- predicted
                    
CM_Function(confusionMatrix(data=predicted, reference = dftest$churn, positive = '1'))

roc_score=roc(factor(dftest$churn, ordered=TRUE), factor(predicted, ordered=TRUE)) #AUC score

auc <- round(auc(factor(dftest$churn, ordered=TRUE), factor(predicted, ordered=TRUE)),4)

ggroc(roc_score, colour = '#0D8387', size = 1) +
 ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + labs(x="1-Specificity", y="Sensitivity")

BAGGING ON ORIGINAL DATA

predicted <- factor(ifelse(predict(ames_bag1, df_original_test[,-c(10)], type = 'prob')[,2] >= 0.5, 1, 0))

PRED_BAGGING_ORIGINAL <- predicted
                    
CM_Function(confusionMatrix(data=predicted, reference = df_original_test$churn, positive = '1'))

roc_score=roc(factor(df_original_test$churn, ordered=TRUE), factor(predicted, ordered=TRUE)) #AUC score

auc <- round(auc(factor(df_original_test$churn, ordered=TRUE), factor(predicted, ordered=TRUE)),4)

ggroc(roc_score, colour = '#0D8387', size = 1) +
 ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + labs(x="1-Specificity", y="Sensitivity")

KNN

set.seed(1)

df <- data.frame(k = seq(1, 30, 1), accuracy = rep(0, 30), sensitivity = rep(0, 30))

# iterating over different ks
for(i in 1:30){
  # nearest neighbor
  KNN1 <- knn3(y = dftrain$churn, x = dftrain[,-c(10)], k = i)

  # predictions response 
  KNN1.pred.valid.resp <- predict(KNN1, dftest[,-c(10)], type = "class")
  
  # predictions prob 
  KNN1.pred.valid.prob <- predict(KNN1, dftest[,-c(10)], type = "prob")[,2]
  
  # Confusionmatrix
  df$sensitivity[i] <- confusionMatrix(KNN1.pred.valid.resp, dftest$churn, positive = "1")$byClass[1]
  df$accuracy[i] <- confusionMatrix(KNN1.pred.valid.resp, dftest$churn, positive = "1")$overall[1]

}

# plot the k's
ggplot(df, aes(x=k)) + 
  geom_line(aes(y = sensitivity, colour = "Sensitivity")) + 
  geom_line(aes(y = accuracy, colour = "Accuracy")) + 
  labs(x = "Number of k nearest neighbours", 
       y = "Accuracy / Sensitivity", title = "Accuracy / Sensitivity regarding k") +
  theme_minimal() + 
  scale_y_continuous(name = "Sensitivity / Accuracy", limits = c(0.7, 1)) +
    scale_color_manual(name = "Values", values = c("Sensitivity" = "darkblue", "Accuracy" = "red")) + 
  xlim (1, 30)

mod.knn <- knn3(y = dftrain$churn, x = dftrain[,-c(10)], k = 2)

predicted.knn <- predict(mod.knn, dftest[,-c(10)], type = "class")

PRED_KNN <- predicted.knn

confmat.knn <- confusionMatrix(data=predicted.knn, reference = dftest$churn, positive = '1')

CM_Function(confmat.knn)

roc_score.qda =roc(factor(dftest$churn, ordered=TRUE), factor(predicted.knn, ordered=TRUE))
plot(roc_score.qda ,main ="ROC curve")

KNN ON ORIGINAL DATA

predicted.knn <- predict(mod.knn, df_original_test[,-c(10)], type = "class")

PRED_KNN_ORIGINAL <- predicted.knn

confmat.knn <- confusionMatrix(data=predicted.knn, reference = df_original_test$churn, positive = '1')

CM_Function(confmat.knn)

roc_score.qda =roc(factor(df_original_test$churn, ordered=TRUE), factor(predicted.knn, ordered=TRUE))
plot(roc_score.qda ,main ="ROC curve")

QDA

mod.qda <- qda(churn ~., data = dftrain)

predicted.qda <- predict(mod.qda, dftest[,-c(10)])$class

confmat.qda <- confusionMatrix(data=predicted.qda, reference = dftest$churn, positive = '1')

CM_Function(confmat.qda)

roc_score.qda =roc(factor(dftest$churn, ordered=TRUE), factor(predicted.qda, ordered=TRUE))
plot(roc_score.qda ,main ="ROC curve")

QDA ON ORIGINAL DATA

predicted.qda <- predict(mod.qda, df_original_test[,-c(10)])$class

confmat.qda <- confusionMatrix(data=predicted.qda, reference = df_original_test$churn, positive = '1')

CM_Function(confmat.qda)

roc_score.qda =roc(factor(df_original_test$churn, ordered=TRUE), factor(predicted.qda, ordered=TRUE))
plot(roc_score.qda ,main ="ROC curve")

QLOG

mod.log <- glm(churn ~., data = dftrain, family = binomial(link = "probit"))

s <- step(mod.log)
FALSE Start:  AIC=6850.25
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_day_calls + total_eve_calls + 
FALSE     total_night_calls + total_intl_calls + number_customer_service_calls + 
FALSE     total_day_charge_per_minute + total_eve_charge_per_minute + 
FALSE     total_night_charge_per_minute + total_intl_charge_per_minute + 
FALSE     account_length_sqd + number_vmail_messages_sqd + total_day_calls_sqd + 
FALSE     total_eve_calls_sqd + total_night_calls_sqd + total_intl_calls_sqd + 
FALSE     number_customer_service_calls_sqd + total_day_charge_per_minute_sqd + 
FALSE     total_eve_charge_per_minute_sqd + total_night_charge_per_minute_sqd + 
FALSE     total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - total_night_calls_sqd              1   6800.3 6848.3
FALSE - account_length_sqd                 1   6800.3 6848.3
FALSE - total_night_calls                  1   6800.4 6848.4
FALSE - total_eve_calls_sqd                1   6800.8 6848.8
FALSE - total_eve_calls                    1   6801.0 6849.0
FALSE - total_day_calls                    1   6801.5 6849.5
FALSE - account_length                     1   6801.7 6849.7
FALSE - total_intl_charge_per_minute       1   6802.2 6850.2
FALSE <none>                                   6800.3 6850.3
FALSE - total_day_calls_sqd                1   6802.3 6850.3
FALSE - total_intl_charge_per_minute_sqd   1   6802.6 6850.6
FALSE - number_vmail_messages_sqd          1   6803.5 6851.5
FALSE - number_vmail_messages              1   6806.4 6854.4
FALSE - total_day_charge_per_minute        1   6807.3 6855.3
FALSE - total_day_charge_per_minute_sqd    1   6807.3 6855.3
FALSE - total_night_charge_per_minute_sqd  1   6807.4 6855.4
FALSE - total_night_charge_per_minute      1   6807.4 6855.4
FALSE - number_customer_service_calls      1   6807.8 6855.8
FALSE - voice_mail_plan                    1   6821.5 6869.5
FALSE - total_intl_calls_sqd               1   6825.6 6873.6
FALSE - total_intl_calls                   1   6828.6 6876.6
FALSE - total_eve_charge_per_minute        1   6831.3 6879.3
FALSE - total_eve_charge_per_minute_sqd    1   6831.3 6879.3
FALSE - number_customer_service_calls_sqd  1   6918.6 6966.6
FALSE - international_plan                 1   7393.9 7441.9
FALSE 
FALSE Step:  AIC=6848.32
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_day_calls + total_eve_calls + 
FALSE     total_night_calls + total_intl_calls + number_customer_service_calls + 
FALSE     total_day_charge_per_minute + total_eve_charge_per_minute + 
FALSE     total_night_charge_per_minute + total_intl_charge_per_minute + 
FALSE     account_length_sqd + number_vmail_messages_sqd + total_day_calls_sqd + 
FALSE     total_eve_calls_sqd + total_intl_calls_sqd + number_customer_service_calls_sqd + 
FALSE     total_day_charge_per_minute_sqd + total_eve_charge_per_minute_sqd + 
FALSE     total_night_charge_per_minute_sqd + total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - account_length_sqd                 1   6800.4 6846.4
FALSE - total_eve_calls_sqd                1   6800.8 6846.8
FALSE - total_eve_calls                    1   6801.0 6847.0
FALSE - total_day_calls                    1   6801.6 6847.6
FALSE - account_length                     1   6801.8 6847.8
FALSE - total_night_calls                  1   6802.0 6848.0
FALSE <none>                                   6800.3 6848.3
FALSE - total_intl_charge_per_minute       1   6802.3 6848.3
FALSE - total_day_calls_sqd                1   6802.4 6848.4
FALSE - total_intl_charge_per_minute_sqd   1   6802.7 6848.7
FALSE - number_vmail_messages_sqd          1   6803.6 6849.6
FALSE - number_vmail_messages              1   6806.4 6852.4
FALSE - total_day_charge_per_minute        1   6807.4 6853.4
FALSE - total_day_charge_per_minute_sqd    1   6807.4 6853.4
FALSE - total_night_charge_per_minute_sqd  1   6807.4 6853.4
FALSE - total_night_charge_per_minute      1   6807.5 6853.5
FALSE - number_customer_service_calls      1   6807.9 6853.9
FALSE - voice_mail_plan                    1   6821.5 6867.5
FALSE - total_intl_calls_sqd               1   6825.6 6871.6
FALSE - total_intl_calls                   1   6828.6 6874.6
FALSE - total_eve_charge_per_minute        1   6831.4 6877.4
FALSE - total_eve_charge_per_minute_sqd    1   6831.4 6877.4
FALSE - number_customer_service_calls_sqd  1   6918.6 6964.6
FALSE - international_plan                 1   7394.5 7440.5
FALSE 
FALSE Step:  AIC=6846.41
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_day_calls + total_eve_calls + 
FALSE     total_night_calls + total_intl_calls + number_customer_service_calls + 
FALSE     total_day_charge_per_minute + total_eve_charge_per_minute + 
FALSE     total_night_charge_per_minute + total_intl_charge_per_minute + 
FALSE     number_vmail_messages_sqd + total_day_calls_sqd + total_eve_calls_sqd + 
FALSE     total_intl_calls_sqd + number_customer_service_calls_sqd + 
FALSE     total_day_charge_per_minute_sqd + total_eve_charge_per_minute_sqd + 
FALSE     total_night_charge_per_minute_sqd + total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - total_eve_calls_sqd                1   6800.9 6844.9
FALSE - total_eve_calls                    1   6801.1 6845.1
FALSE - total_day_calls                    1   6801.6 6845.6
FALSE - total_night_calls                  1   6802.1 6846.1
FALSE <none>                                   6800.4 6846.4
FALSE - total_intl_charge_per_minute       1   6802.4 6846.4
FALSE - total_day_calls_sqd                1   6802.4 6846.4
FALSE - total_intl_charge_per_minute_sqd   1   6802.8 6846.8
FALSE - number_vmail_messages_sqd          1   6803.6 6847.6
FALSE - number_vmail_messages              1   6806.5 6850.5
FALSE - total_day_charge_per_minute        1   6807.4 6851.4
FALSE - total_day_charge_per_minute_sqd    1   6807.5 6851.5
FALSE - total_night_charge_per_minute_sqd  1   6807.5 6851.5
FALSE - total_night_charge_per_minute      1   6807.5 6851.5
FALSE - number_customer_service_calls      1   6808.0 6852.0
FALSE - account_length                     1   6813.6 6857.6
FALSE - voice_mail_plan                    1   6821.6 6865.6
FALSE - total_intl_calls_sqd               1   6825.8 6869.8
FALSE - total_intl_calls                   1   6828.8 6872.8
FALSE - total_eve_charge_per_minute        1   6831.5 6875.5
FALSE - total_eve_charge_per_minute_sqd    1   6831.5 6875.5
FALSE - number_customer_service_calls_sqd  1   6918.8 6962.8
FALSE - international_plan                 1   7395.7 7439.7
FALSE 
FALSE Step:  AIC=6844.91
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_day_calls + total_eve_calls + 
FALSE     total_night_calls + total_intl_calls + number_customer_service_calls + 
FALSE     total_day_charge_per_minute + total_eve_charge_per_minute + 
FALSE     total_night_charge_per_minute + total_intl_charge_per_minute + 
FALSE     number_vmail_messages_sqd + total_day_calls_sqd + total_intl_calls_sqd + 
FALSE     number_customer_service_calls_sqd + total_day_charge_per_minute_sqd + 
FALSE     total_eve_charge_per_minute_sqd + total_night_charge_per_minute_sqd + 
FALSE     total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - total_eve_calls                    1   6802.0 6844.0
FALSE - total_day_calls                    1   6802.1 6844.1
FALSE - total_night_calls                  1   6802.6 6844.6
FALSE - total_day_calls_sqd                1   6802.9 6844.9
FALSE - total_intl_charge_per_minute       1   6802.9 6844.9
FALSE <none>                                   6800.9 6844.9
FALSE - total_intl_charge_per_minute_sqd   1   6803.2 6845.2
FALSE - number_vmail_messages_sqd          1   6804.1 6846.1
FALSE - number_vmail_messages              1   6807.0 6849.0
FALSE - total_day_charge_per_minute        1   6807.9 6849.9
FALSE - total_day_charge_per_minute_sqd    1   6808.0 6850.0
FALSE - total_night_charge_per_minute_sqd  1   6808.0 6850.0
FALSE - total_night_charge_per_minute      1   6808.0 6850.0
FALSE - number_customer_service_calls      1   6808.5 6850.5
FALSE - account_length                     1   6814.1 6856.1
FALSE - voice_mail_plan                    1   6822.1 6864.1
FALSE - total_intl_calls_sqd               1   6826.1 6868.1
FALSE - total_intl_calls                   1   6829.0 6871.0
FALSE - total_eve_charge_per_minute        1   6832.0 6874.0
FALSE - total_eve_charge_per_minute_sqd    1   6832.0 6874.0
FALSE - number_customer_service_calls_sqd  1   6919.2 6961.2
FALSE - international_plan                 1   7397.0 7439.0
FALSE 
FALSE Step:  AIC=6844
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_day_calls + total_night_calls + 
FALSE     total_intl_calls + number_customer_service_calls + total_day_charge_per_minute + 
FALSE     total_eve_charge_per_minute + total_night_charge_per_minute + 
FALSE     total_intl_charge_per_minute + number_vmail_messages_sqd + 
FALSE     total_day_calls_sqd + total_intl_calls_sqd + number_customer_service_calls_sqd + 
FALSE     total_day_charge_per_minute_sqd + total_eve_charge_per_minute_sqd + 
FALSE     total_night_charge_per_minute_sqd + total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - total_day_calls                    1   6803.2 6843.2
FALSE - total_night_calls                  1   6803.7 6843.7
FALSE - total_intl_charge_per_minute       1   6804.0 6844.0
FALSE - total_day_calls_sqd                1   6804.0 6844.0
FALSE <none>                                   6802.0 6844.0
FALSE - total_intl_charge_per_minute_sqd   1   6804.3 6844.3
FALSE - number_vmail_messages_sqd          1   6805.2 6845.2
FALSE - number_vmail_messages              1   6808.1 6848.1
FALSE - total_day_charge_per_minute        1   6809.0 6849.0
FALSE - total_day_charge_per_minute_sqd    1   6809.0 6849.0
FALSE - total_night_charge_per_minute_sqd  1   6809.0 6849.0
FALSE - total_night_charge_per_minute      1   6809.0 6849.0
FALSE - number_customer_service_calls      1   6809.5 6849.5
FALSE - account_length                     1   6815.3 6855.3
FALSE - voice_mail_plan                    1   6823.3 6863.3
FALSE - total_intl_calls_sqd               1   6826.9 6866.9
FALSE - total_intl_calls                   1   6829.8 6869.8
FALSE - total_eve_charge_per_minute        1   6833.4 6873.4
FALSE - total_eve_charge_per_minute_sqd    1   6833.4 6873.4
FALSE - number_customer_service_calls_sqd  1   6919.9 6959.9
FALSE - international_plan                 1   7397.2 7437.2
FALSE 
FALSE Step:  AIC=6843.19
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_night_calls + total_intl_calls + 
FALSE     number_customer_service_calls + total_day_charge_per_minute + 
FALSE     total_eve_charge_per_minute + total_night_charge_per_minute + 
FALSE     total_intl_charge_per_minute + number_vmail_messages_sqd + 
FALSE     total_day_calls_sqd + total_intl_calls_sqd + number_customer_service_calls_sqd + 
FALSE     total_day_charge_per_minute_sqd + total_eve_charge_per_minute_sqd + 
FALSE     total_night_charge_per_minute_sqd + total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - total_night_calls                  1   6804.8 6842.8
FALSE - total_intl_charge_per_minute       1   6805.2 6843.2
FALSE <none>                                   6803.2 6843.2
FALSE - total_intl_charge_per_minute_sqd   1   6805.5 6843.5
FALSE - number_vmail_messages_sqd          1   6806.5 6844.5
FALSE - total_day_calls_sqd                1   6808.6 6846.6
FALSE - number_vmail_messages              1   6809.4 6847.4
FALSE - total_day_charge_per_minute        1   6810.4 6848.4
FALSE - total_day_charge_per_minute_sqd    1   6810.4 6848.4
FALSE - total_night_charge_per_minute_sqd  1   6810.4 6848.4
FALSE - total_night_charge_per_minute      1   6810.4 6848.4
FALSE - number_customer_service_calls      1   6810.5 6848.5
FALSE - account_length                     1   6816.6 6854.6
FALSE - voice_mail_plan                    1   6824.6 6862.6
FALSE - total_intl_calls_sqd               1   6827.8 6865.8
FALSE - total_intl_calls                   1   6830.7 6868.7
FALSE - total_eve_charge_per_minute        1   6834.6 6872.6
FALSE - total_eve_charge_per_minute_sqd    1   6834.6 6872.6
FALSE - number_customer_service_calls_sqd  1   6920.5 6958.5
FALSE - international_plan                 1   7399.3 7437.3
FALSE 
FALSE Step:  AIC=6842.78
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_intl_calls + number_customer_service_calls + 
FALSE     total_day_charge_per_minute + total_eve_charge_per_minute + 
FALSE     total_night_charge_per_minute + total_intl_charge_per_minute + 
FALSE     number_vmail_messages_sqd + total_day_calls_sqd + total_intl_calls_sqd + 
FALSE     number_customer_service_calls_sqd + total_day_charge_per_minute_sqd + 
FALSE     total_eve_charge_per_minute_sqd + total_night_charge_per_minute_sqd + 
FALSE     total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE - total_intl_charge_per_minute       1   6806.7 6842.7
FALSE <none>                                   6804.8 6842.8
FALSE - total_intl_charge_per_minute_sqd   1   6807.1 6843.1
FALSE - number_vmail_messages_sqd          1   6808.1 6844.1
FALSE - total_day_calls_sqd                1   6810.2 6846.2
FALSE - number_vmail_messages              1   6811.0 6847.0
FALSE - number_customer_service_calls      1   6811.8 6847.8
FALSE - total_day_charge_per_minute        1   6812.2 6848.2
FALSE - total_day_charge_per_minute_sqd    1   6812.3 6848.3
FALSE - total_night_charge_per_minute_sqd  1   6812.3 6848.3
FALSE - total_night_charge_per_minute      1   6812.3 6848.3
FALSE - account_length                     1   6818.0 6854.0
FALSE - voice_mail_plan                    1   6826.2 6862.2
FALSE - total_intl_calls_sqd               1   6829.3 6865.3
FALSE - total_intl_calls                   1   6832.2 6868.2
FALSE - total_eve_charge_per_minute        1   6836.2 6872.2
FALSE - total_eve_charge_per_minute_sqd    1   6836.2 6872.2
FALSE - number_customer_service_calls_sqd  1   6921.0 6957.0
FALSE - international_plan                 1   7402.7 7438.7
FALSE 
FALSE Step:  AIC=6842.74
FALSE churn ~ account_length + international_plan + voice_mail_plan + 
FALSE     number_vmail_messages + total_intl_calls + number_customer_service_calls + 
FALSE     total_day_charge_per_minute + total_eve_charge_per_minute + 
FALSE     total_night_charge_per_minute + number_vmail_messages_sqd + 
FALSE     total_day_calls_sqd + total_intl_calls_sqd + number_customer_service_calls_sqd + 
FALSE     total_day_charge_per_minute_sqd + total_eve_charge_per_minute_sqd + 
FALSE     total_night_charge_per_minute_sqd + total_intl_charge_per_minute_sqd
FALSE 
FALSE                                     Df Deviance    AIC
FALSE <none>                                   6806.7 6842.7
FALSE - number_vmail_messages_sqd          1   6810.0 6844.0
FALSE - total_day_calls_sqd                1   6812.2 6846.2
FALSE - number_vmail_messages              1   6812.8 6846.8
FALSE - number_customer_service_calls      1   6813.8 6847.8
FALSE - total_day_charge_per_minute        1   6814.3 6848.3
FALSE - total_night_charge_per_minute_sqd  1   6814.3 6848.3
FALSE - total_day_charge_per_minute_sqd    1   6814.3 6848.3
FALSE - total_night_charge_per_minute      1   6814.4 6848.4
FALSE - account_length                     1   6819.8 6853.8
FALSE - total_intl_charge_per_minute_sqd   1   6823.8 6857.8
FALSE - voice_mail_plan                    1   6827.9 6861.9
FALSE - total_intl_calls_sqd               1   6831.0 6865.0
FALSE - total_intl_calls                   1   6833.8 6867.8
FALSE - total_eve_charge_per_minute        1   6837.9 6871.9
FALSE - total_eve_charge_per_minute_sqd    1   6838.0 6872.0
FALSE - number_customer_service_calls_sqd  1   6922.8 6956.8
FALSE - international_plan                 1   7403.2 7437.2
mod.log <- glm(s$formula, data = dftrain, family = binomial(link = "probit"))

predicted.log <- factor(ifelse(predict(mod.log, dftest[,-c(10)], type='response')>0.5,1,0))

confmat.log <- confusionMatrix(data=predicted.log, reference = dftest$churn, positive = '1')

CM_Function(confmat.log)

roc_score.log =roc(factor(dftest$churn, ordered=TRUE), factor(predicted.log, ordered=TRUE))
plot(roc_score.log ,main ="ROC curve")

GAUSSIAN SVM

library(e1071)
mod.svm  = svm(formula = churn ~ .,
               data = dftrain,
                 type = 'C-classification', # this is because we want to make a regression classification
                 kernel = 'radial')

predicted.svm <- predict(mod.svm, dftest[,-c(10)])

confmat.svm <- confusionMatrix(data=predicted.svm, reference = dftest$churn, positive = '1')

CM_Function(confmat.svm)

roc_score.svm =roc(factor(dftest$churn, ordered=TRUE), factor(predicted.svm, ordered=TRUE))
plot(roc_score.svm ,main ="ROC curve")

ENSEMBLES - MAJORITY VOTING

BOOSTING CTREE BAGGING

ENSEMBLES <- cbind(PRED_BOOSTING,PRED_CTREE,PRED_BAGGING)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES <- ifelse(ENSEMBLES == 2, 1, 0)
MAJORITY_VOTE <- rep(0,nrow(ENSEMBLES))


MAJORITY_VOTE <- ifelse(rowSums(ENSEMBLES) > (ncol(ENSEMBLES)-1)/2, 1, 0)

ENSEMBLES <- cbind(ENSEMBLES,MAJORITY_VOTE)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES$MAJORITY_VOTE <- as.factor(ENSEMBLES$MAJORITY_VOTE)

CM_ENSEMBLES <- confusionMatrix(data=ENSEMBLES$MAJORITY_VOTE, reference = dftest$churn, positive = '1')

CM_Function(CM_ENSEMBLES)

roc_score =roc(factor(dftest$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE))

auc <- round(auc(factor(dftest$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE)),4)

ggroc(roc_score, colour = '#0D8387', size = 1) +
 ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + labs(x="1-Specificity", y="Sensitivity")

ENSEMBLES - MAJORITY VOTING ON ORIGINAL

BOOSTING CTREE BAGGING

ENSEMBLES <- cbind(PRED_BOOSTING_ORIGINAL,PRED_CTREE_ORIGINAL,PRED_BAGGING_ORIGINAL)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES <- ifelse(ENSEMBLES == 2, 1, 0)
MAJORITY_VOTE <- rep(0,nrow(ENSEMBLES))


MAJORITY_VOTE <- ifelse(rowSums(ENSEMBLES) > (ncol(ENSEMBLES)-1)/2, 1, 0)

ENSEMBLES <- cbind(ENSEMBLES,MAJORITY_VOTE)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES$MAJORITY_VOTE <- as.factor(ENSEMBLES$MAJORITY_VOTE)

CM_ENSEMBLES <- confusionMatrix(data=ENSEMBLES$MAJORITY_VOTE, reference = df_original_test$churn, positive = '1')

CM_Function(CM_ENSEMBLES)

roc_score =roc(factor(df_original_test$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE))

auc <- round(auc(factor(df_original_test$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE)),4)

ggroc(roc_score, colour = '#0D8387', size = 1) +
 ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + labs(x="1-Specificity", y="Sensitivity")

ENSEMBLES - MAJORITY VOTING

KNN CTREE BAGGING

ENSEMBLES <- cbind(PRED_KNN,PRED_CTREE,PRED_BAGGING)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES <- ifelse(ENSEMBLES == 2, 1, 0)
MAJORITY_VOTE <- rep(0,nrow(ENSEMBLES))


MAJORITY_VOTE <- ifelse(rowSums(ENSEMBLES) > (ncol(ENSEMBLES)-1)/2, 1, 0)

ENSEMBLES <- cbind(ENSEMBLES,MAJORITY_VOTE)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES$MAJORITY_VOTE <- as.factor(ENSEMBLES$MAJORITY_VOTE)

CM_ENSEMBLES <- confusionMatrix(data=ENSEMBLES$MAJORITY_VOTE, reference = dftest$churn, positive = '1')

CM_Function(CM_ENSEMBLES)

roc_score =roc(factor(dftest$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE))

auc <- round(auc(factor(dftest$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE)),4)

ggroc(roc_score, colour = '#0D8387', size = 1) +
 ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + labs(x="1-Specificity", y="Sensitivity")

ENSEMBLES - MAJORITY VOTING ON ORIGINAL

KNN CTREE BAGGING

ENSEMBLES <- cbind(PRED_KNN_ORIGINAL,PRED_CTREE_ORIGINAL,PRED_BAGGING_ORIGINAL)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES <- ifelse(ENSEMBLES == 2, 1, 0)
MAJORITY_VOTE <- rep(0,nrow(ENSEMBLES))


MAJORITY_VOTE <- ifelse(rowSums(ENSEMBLES) > (ncol(ENSEMBLES)-1)/2, 1, 0)

ENSEMBLES <- cbind(ENSEMBLES,MAJORITY_VOTE)
ENSEMBLES <- as.data.frame(ENSEMBLES)
ENSEMBLES$MAJORITY_VOTE <- as.factor(ENSEMBLES$MAJORITY_VOTE)

CM_ENSEMBLES <- confusionMatrix(data=ENSEMBLES$MAJORITY_VOTE, reference = df_original_test$churn, positive = '1')

CM_Function(CM_ENSEMBLES)

roc_score =roc(factor(df_original_test$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE))

auc <- round(auc(factor(df_original_test$churn, ordered=TRUE), factor(ENSEMBLES$MAJORITY_VOTE, ordered=TRUE)),4)

ggroc(roc_score, colour = '#0D8387', size = 1) +
 ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + labs(x="1-Specificity", y="Sensitivity")

PRESENTATION ASSETS

rm(list = ls())
# READ DATA
df <- readxl::read_xls('Cchurn.xls')
df$international_plan <- factor(df$international_plan, levels = c('no', 'yes'), labels = c('0','1'))
df$voice_mail_plan <- factor(df$voice_mail_plan, levels = c('no', 'yes'), labels = c('0','1'))
df$churn <- factor(df$churn, levels = c('no', 'yes'), labels = c('No','Yes'))

# DATA ENGINEERING
df$total_day_charge_per_minute <- ifelse(df$total_day_minutes == 0, 0, df$total_day_charge / df$total_day_minutes)
df$total_eve_charge_per_minute <- ifelse(df$total_eve_minutes == 0, 0, df$total_eve_charge / df$total_eve_minutes)
df$total_night_charge_per_minute <- ifelse(df$total_night_minutes == 0, 0, df$total_night_charge / df$total_night_minutes)
df$total_intl_charge_per_minute <- ifelse(df$total_intl_minutes == 0, 0, df$total_intl_charge / df$total_intl_minutes)
df <- subset(df, select = -c(total_day_charge, total_day_minutes, total_eve_charge, total_eve_minutes, total_night_charge, total_night_minutes, total_intl_charge, total_intl_minutes))

colnames(df) <- c("Account Length", "International Plan","Voice Mail Plan","Voice Mail Messages","Total Days Calls","Total Evening Calls","Total Night Calls","Total Internation Calls","Total Customer Service Call","Customer Churn","Total Day Charge/Minute", "Total Evening Charge/Minute","Total Night Charge/Minute","Total International Charge/Minute")

PAIRS PLOT

# Load the necessary libraries
library(ggplot2)
library(GGally)
library(ggthemes)

# Set the main color palette
colors <- c("#0D8387", "#870D27")

# Create the ggpair plot 
PAIRS1 <- ggpairs(df,columns = c(1:4), mapping = aes(col = `Customer Churn`, alpha = 0.9)) + scale_color_manual(values = colors) + scale_fill_manual(values = colors) + labs(title = "Customer Telecommunication Data", subtitle = "Customer Churn = Yes  is red", caption="From Variable 1 to 4") + theme(plot.title = element_text(face = "bold")) 

PAIRS1

PAIRS2 <- ggpairs(df,columns = c(5:9), mapping = aes(col = `Customer Churn`, alpha = 0.9)) + scale_color_manual(values = colors) + scale_fill_manual(values = colors) + labs(title = "Customer Telecommunication Data", subtitle = "Customer Churn = Yes  is red", caption="From Variable 5 to 9") + theme(plot.title = element_text(face = "bold"))

PAIRS2

PAIRS3 <- ggpairs(df,columns = c(10:10), mapping = aes(col = `Customer Churn`, alpha = 0.9)) + scale_color_manual(values = colors) + scale_fill_manual(values = colors) + labs(title = "Customer Telecommunication Data", subtitle = "", y="Count") + theme(plot.title = element_text(face = "bold")) + theme_minimal() + theme(plot.title = element_text(face = "bold")) + annotate("text", x = 2, y = 900, label = "14.14%", colour = "#870D27", size=8) + annotate("text", x = 1, y = 4500, label = "85.86%", colour = "#0D8387", size=8) + theme(axis.text.x=element_text(size=16))

PAIRS3

PAIRS4 <- ggpairs(df,columns = c(11:14), mapping = aes(col = `Customer Churn`, alpha = 0.9)) + scale_color_manual(values = colors) + scale_fill_manual(values = colors) + labs(title = "Customer Telecommunication Data", subtitle = "Customer Churn = Yes  is red",caption="From Variable 11 to 14") + theme(plot.title = element_text(face = "bold"))

PAIRS4

df$`Customer Churn` <- factor(df$`Customer Churn`, levels = c('No', 'Yes'), labels = c(0,1))

df$`Customer Churn` <- as.integer(df$`Customer Churn`)

df$`Customer Churn` <- df$`Customer Churn` -1 

Proportions_Churn <- sum(df$`Customer Churn`[df$`Customer Churn` == 1])/nrow(df)
Proportions_No_Churn <- 1-Proportions_Churn

Proportions of Customer who churned => 14.14% Versus 85.86% who didn’t churn.